package com.computaris.ws;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.context.ManagedSessionContext;

public class EventImpl {
	
	public Exception createEvent(Event event) {
		Session session = createNewSessionAndTransaction();
		try{
			session.save(event);
		}
		catch(Exception e){
			rollbackTransaction(session);
			return e;
		}
		
		commitTransaction(session);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Event> getAllEvents() {
		Session session = createNewSessionAndTransaction();
		if(session == null) {
			return null;
		}
		Query query = session.createQuery("from Event");

		// try to find subscriber
		List<Event> events = null;
		try{
			List<?> list = query.list();
			if(list != null && list.size() > 0 ){
				events = (List<Event>) list;
			}
			else {
			}
		}catch(Exception e){
			rollbackTransaction(session);
			return events;
		}

		commitTransaction(session);
		return events;
	}
	
	@SuppressWarnings("unchecked")
	public List<Event> getEvents(String msisdn) {

//		Event sd = new Event();
	//	sd.setStatus(1);

		Session session = createNewSessionAndTransaction();

		if(session == null) {
			return null;
		}
		
		Query query = session.createQuery("from Event where SUBSCRIBER_ID = :msisdn ")
				.setParameter("msisdn", msisdn);

		// try to find subscriber
		List<Event> events = null;
		try{
			List<?> list = query.list();
			if(list != null && list.size() > 0 ){
//				sd.setStatus(0);
				events = (List<Event>) list;
//				sd.setSubscriberDetails(event);
			}
			else {
				events = new ArrayList<Event>();
//				sd.setStatus(1);
//				sd.setDescription("Subscriber with msisdn: " + msisdn + " is not defined in DB.");    			
			}
		}catch(Exception e){
//			sd.setDescription(e.getMessage());
			rollbackTransaction(session);
			return events;
		}

		commitTransaction(session);
		return events;
	} 


	protected org.hibernate.Session createNewSession() {
		org.hibernate.classic.Session session = SessionFactoryHelper.getSessionFactory().openSession();
		session.setFlushMode(FlushMode.MANUAL);
		ManagedSessionContext.bind(session);
		return (org.hibernate.Session) session;
	}

	/**
	 * Start a new Transaction in the given session
	 * @param session The session to create the transaction in
	 */
	protected void startNewTransaction(Session session) {
		session.beginTransaction();
	}

	/**
	 * Shortcut method that creates a new session and begins a transaction in it
	 * @return A new session with a transaction started
	 */
	protected org.hibernate.Session createNewSessionAndTransaction() {
		try {
			Session session = createNewSession();
			startNewTransaction(session);
			return session;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * Commit the transaction within the given session. This method unbinds
	 * the session from the session context (ManagedSessionContext), flushes
	 * the session, commmits the session and then closes the session
	 * @param session The session with the transaction to commit
	 */
	protected void commitTransaction(Session session) {
		ManagedSessionContext.unbind(SessionFactoryHelper.getSessionFactory());
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	protected void rollbackTransaction(Session session) {
		ManagedSessionContext.unbind(SessionFactoryHelper.getSessionFactory());
		session.getTransaction().rollback();
		session.close();
	}
}
